<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import { artGetListService } from '@/api/article'
import { formatTime } from '@/utils/format.js'
import ArticleEdit from './components/ArticleEdit.vue'

const title = ref('文章管理')

const parmas = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})

const artDataList = ref([])
const total = ref(0)
const loading = ref(false)
const articleEditRef = ref(null)

const getArticleList = async () => {
  loading.value = true

  const res = await artGetListService(parmas.value)
  artDataList.value = res.data.data
  total.value = res.data.total

  loading.value = false
}

getArticleList()

const onAddArticle = () => {
  articleEditRef.value.open({})
}

const onSizeChange = (size) => {
  parmas.value.pagenum = 1
  parmas.value.pagesize = size
  getArticleList()
}

const onCurrentChange = (page) => {
  parmas.value.pagenum = page
  getArticleList()
}

const onSearch = () => {
  parmas.value.pagenum = 1
  getArticleList()
}

const onRestart = () => {
  parmas.value.pagenum = 1
  parmas.value.cate_id = ''
  parmas.value.state = ''
  getArticleList()
}

// 删除文章
const onDelArticle = (row) => {
  console.log(row)
}

// 编辑文章
const onEditArticle = (row) => {
  articleEditRef.value.open(row)
}

const onSuccess = (type) => {
  if (type === 'add') {
    // 添加，渲染最后一页
    const lastPage = Math.ceil((total.value + 1) / parmas.value.pagesize)
    parmas.value.pagenum = lastPage
  }

  getArticleList()
}
</script>

<template>
  <page-container :title="title" style="overflow: scroll">
    <template #extra>
      <el-button @click="onAddArticle" type="primary">发布文章</el-button>
    </template>
    <template #default>
      <!-- 表单区域 -->
      <el-form inline>
        <el-form-item label="文章分类">
          <channel-select
            width="200px"
            v-model="parmas.cate_id"
          ></channel-select>
        </el-form-item>
        <el-form-item label="发布状态">
          <el-select
            v-model="parmas.state"
            style="width: 200px"
            placeholder="请选择"
          >
            <el-option label="已发布" value="已发布"></el-option>
            <el-option label="草稿" value="草稿"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="onSearch" type="primary">搜索</el-button>
          <el-button @click="onRestart" plain>重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格区域 -->
      <el-table :data="artDataList" v-loading="loading">
        <el-table-column label="文章标题">
          <template #default="{ row }">
            <el-link :underline="false" type="primary">{{ row.title }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="分类" prop="cate_name"></el-table-column>
        <el-table-column label="发布时间" prop="pub_date">
          <template #default="{ row }">
            {{ formatTime(row.pub_date) }}
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="state"></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              plain
              circle
              type="primary"
              @click="onEditArticle(row)"
              :icon="Edit"
            ></el-button>
            <el-button
              plain
              circle
              :icon="Delete"
              type="danger"
              @click="onDelArticle(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="parmas.pagenum"
        v-model:page-size="parmas.pagesize"
        :page-sizes="[2, 3, 5, 10]"
        :background="true"
        layout="jumper, total, sizes, prev, pager, next"
        :total="total"
        @size-change="onSizeChange"
        @current-change="onCurrentChange"
        style="margin-top: 20px; justify-content: end"
      />
    </template>
  </page-container>
  <article-edit ref="articleEditRef" @success="onSuccess"></article-edit>
</template>

<style scoped></style>
